package com.lanou.utils;


import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by dllo on 18/3/10.
 */
public class StrUtils {
    public static boolean isNull(String str) {
        if (str.isEmpty() || str.length() == 0) {
            return true;
        } else {
            return false;
        }

    }

    public static boolean isMailRight(String str) {
        return regTest(str, 1);
    }

    public static boolean isPhoneNumberRight(String str) {
        return regTest(str, 0);
    }

    public static boolean regTest(String str, Integer regTyep) {
        String regEx = "";
        if (regTyep == 1) {
            regEx = "[0-9a-zA-Z_]{1,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
        } else {
            regEx = "^1[3|4|5|7|8][0-9]\\d{8}$";
        }
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static String charSetStr(String str) {

        if (str != null) {
            Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
            Matcher m = p.matcher(str);
            if (!m.find()) {
                try {
                    return new String(str.getBytes("ISO-8859-1"), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    return str;
                }
            } else {
                return str;
            }
        } else {
            return null;
        }

    }


}
